let list = require("./list.js")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isOpen: true,
        url: "",
        tempPhoto: "",
        statusBarHeight: "",
        list: [],
        origin_color: "#2f8ff6",
        filter_degree: "80",
        id:"",
       
        isSure: false,
        list:[]

    },
    /**
     * 生命周期函数--监听页面显示
     */

    onLoad() {
       
        let tempPhoto = getApp().globalData.tempPhoto
        // console.log(list)
        wx.getSystemInfo({
            success: res => {
                // console.log(res)
                this.setData({
                    photoHeight: res.screenHeight - 230 + "px",
                    statusBarHeight: res.statusBarHeight,
                    url: tempPhoto,
                    tempPhoto: tempPhoto,
                    list:list.default.list
                })
            },
        })

    },


    previewImg() {
        wx.previewImage({
            urls: [this.data.url],
        })


    },
    handle_filter_degree(e) {
        let filter_degree = e.detail.value
        this.setData({
            filter_degree: filter_degree,
        })

        if(this.data.id){
            this.setData({
                isSure: true,
            })
        }
    },
    
    exchange_degree(e){
        let list = JSON.parse(JSON.stringify(this.data.list))
        list.forEach(item => {
            item.select = false
        })
        list.forEach(item => {
            if (item.id == e.currentTarget.dataset.item.id) {
                item.select = true
            }
        })
        this.setData({
            list: list,
            id:e.currentTarget.dataset.item.id,
            isSure:true
         
        })
    },
   

    fgqd() {
        if (this.data.isSure) {
            wx.showLoading({
              title: '解析中...',
            })
            wx.getFileSystemManager().readFile({
                filePath: this.data.tempPhoto, //选择图片返回的相对路径
                encoding: 'base64', //编码格式
                success: res => { //成功的回调
                    let data = {
                        openid: wx.getStorageSync('ACCESS_TOKEN'),
                        nickname: wx.getStorageSync('USER_INFO').nickname,
                        avatar: wx.getStorageSync('USER_INFO').avatar_url,
                        unionid: wx.getStorageSync('USER_INFO').union_id,
                        base64String: res.data,
                        filter_degree: this.data.filter_degree+"",
                        filter_type:this.data.id
                       
                    }

                    wx.request({
                        method: "POST",
                        url: getApp().api.bmmj_mnfg,
                        data: data,
                        header: {
                            'content-type': 'application/json'
                        },
                        success: (result => {
                            // console.log("res", result)
                            wx.hideLoading()
                            if (result.data.code == '0') {
                                this.setData({
                                    url:result.data.style_img_url
                                })

                            } else {
                                // console.log(result.data)
                                // let msg = result.data.msg
                                wx.showToast({
                                    title: "图片中没有人脸。",
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        }),
                        fail: (fail => {
                            wx.hideLoading()
                            // console.log("err", fail)
                        })
                    })


                }
            })





        }
    },

    naviback() {
        wx.navigateBack({
            delta: 1,
        })
    }
})